package com.dk.shiro;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.context.annotation.Bean;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class LoginRealm extends AuthorizingRealm {
   //授权逻辑的方法
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("授权方法");
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        List<String> perms = new ArrayList<>();
        perms.add("delete");
        info.addStringPermissions(perms);

        return info;
    }
    //编写认证逻辑的方法（登录）
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("认证方法");
        String userName = "admin";
        String password = "17471e854c0fa92c996b04ac2b49002d";//1234
        String salt = "911181";
        UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken;

       if (!Objects.equals(userName,token.getUsername())){
           return null;
       }
       //参数位置 1 , 登录后的用户对象。2,3:数据库查询出来的用户名和密码
        return new SimpleAuthenticationInfo(userName,password, ByteSource.Util.bytes(salt),userName);
    }

}
